/*
 * Copyright (c) 2011 Seoul National University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jung Ho Ahn  ( gajh@snu.ac.kr )
 *
 */

#ifndef __KMERNODE_H__
#define __KMERNODE_H__

#include <cstdint>
#include "Kmer.h"
#include "Node.h"


// conventions
//  backward == prev == left  == pushFromMSB()
//  forward  == next == right == pushFromLSB()
class KmerNode : public Node
{
  struct NodeHitcountPair
  {
    NodeHitcountPair() :node(NULL), hitcount(0.0) { }
    Node * node;
    float  hitcount;
  };

  union NodeOrNodes
  {
    Node             * node;
    NodeHitcountPair * nodes;

    NodeOrNodes(Node * node_)              : node(node_)   { };
    NodeOrNodes(NodeHitcountPair * nodes_) : nodes(nodes_) { };
  };

  public:
    KmerNode(Kmer * kmer, uint64_t id_, uint64_t hitcount_ = 1, bool isKmerRC_ = false);
    ~KmerNode();
    inline uint32_t getHitCount() const { return hitcount; }
    inline Kmer * getKmer() const { return kmer; }
    Node * getPrev(nucleotide_type nucleotide);
    Node * getNext(nucleotide_type nucleotide);
    void setPrev(Node * node, nucleotide_type nucleotide);
    void setNext(Node * node, nucleotide_type nucleotide);
    //void * operator new(uint64_t num_bytes, uint64_t memAddr) { return (void *)memAddr; }

  //private:
    Kmer *kmer;
    uint32_t hitcount : 32;
    nucleotide_type prev_type : 4;
    nucleotide_type next_type : 4;
    int numPrev : 4;
    int numNext : 4;
    bool isKmerRC : 1;
    NodeOrNodes prev;
    NodeOrNodes next;

  friend class ContigNode;
  friend class KmerCoverageTable;
  friend class Graph;
  friend void * initializeContigsParallel(void *);
  friend void * initializeContigsParallelTight(void *);
  friend ostream & operator<<(ostream & out, const KmerNode & k);
};
#endif
